<?php

namespace App\Console\Commands;

use App\Models\Goods;
use App\Models\GoodsPrice;
use Illuminate\Console\Command;

class AutoAddGoodsPrice extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:autoaddgoodsprice';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description:自动添加产品价格';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $goodsList = Goods::all();
        foreach($goodsList as $k=>$v){
            $first = GoodsPrice::where('goods_id',$v->id)->orderBy('now_date','desc')->first();
            GoodsPrice::updateOrCreate(
                ['now_date'=>date("Y-m-d"),'goods_id'=>$v->id],
                ['now_date'=>date("Y-m-d"),'goods_id'=>$v->id,'p_price'=>$first->p_price,'s_price'=>$first->s_price]
            );
        }
    }
}
